/*
 * 自定义modelTree初始化
 */
;
(function($) {
	$.extend(true, window, {
		"BaseModelTree" : {
			"init" : init,
			"detailEdit":_detailEdit,
			"importModel" : _importModel,
			"addModelCategory" : _baraddModelCategory,
			"addModel" : _baraddModel,
			"resetModelDetail" : _resetModelDetail
		}
	});
	var op = {
		selector : "div#configtree",
		formSelector :"#config_detail",
		navTabId : null,
		checkCallback : function() {
		}
	};
	$this = this;

	function init(initop) {
		op = $.extend(op, initop);
		_contextmenu($(op.selector + " li"));
		showDetails();
	}

	/**
	 * 右键菜单初始化
	 */
	function _contextmenu($obj) {
		
		function _getChildren($obj) {
			return $obj.children("div").children("a");
		}

		function _addConfig(t) {
			$("#config_detail").show();
		}

		function _delConfig(t) {
			alertMsg.info("删除节点");
		}
		
		$obj.contextMenu('configTreeZH', {
			shadow : false,
			bindings : {
				addConf : function(t) {
					_addConfig(t);
				},
				delConf : function(t) {
					_delConfig(t);
				}
			},
			ctrSub : function(t, m) {
				/**
				 * 获取当级树的内容
				 */
				var $a = _getChildren(t);
				var _id = $a.attr("id");
				/*
				 * 判断是否存在code
				 */
				if(typeof _id ==="undefined" || _id ===null){
					_id = "";
				}
				if(_id === ""){
					m.find("[rel='delModel']").hide();
				}else{
					m.find("[rel='addModelCategory']").hide();
					m.find("[rel='addModel']").hide();
					m.find("[rel='modModelCategory']").hide();
				}
			}
		});
	}
	/**
	 * 详情展示
	 */
	function showDetails() {
		$(op.selector).find(".details").unbind();
		$("a[target=ajax].details", $(op.selector)).each(function() {
			$(this).click(function(event) {
				var $this = $(this);
				var rel = $this.attr("rel");
				if (rel) {
					var $rel = $("#" + rel);
					$rel.loadUrl($this.attr("href"), {}, function() {
						$rel.find("[layoutH]").layoutH();
					});
				}
				event.preventDefault();
			});
		});
	}
	/*
	 * $.extend(this,{ "init":init });
	 */
	
	
	/**
	 * 课程编辑按钮点击触发的事件
	 */
	function _detailEdit(obj){
		$(op.formSelector+" #itemLable").removeAttr("readonly");
		$(op.formSelector+" #checkbox").removeAttr("readonly");
		$(op.formSelector+" #sortOrder").removeAttr("readonly");
		$(op.formSelector+" #description").removeAttr("readonly");
		$(obj).parent().html('<div class="button"><div class="buttonContent"><button type="submit">更新</button></div></div><div class="button"   onclick="BaseCourseTree.returnDetail(this);"><div class="buttonContent"><button type="button">返回</button></div></div>');
	}
	
	function _importModel() {
		var tree = $("div#modeltree");
		var $a = tree.find("div.selected a");
		if($a){
			var category = $a.attr("category");
			if(!category){
				alertMsg.error("请选择类型节点进行操作!");
				return;
			}else{
				$.pdialog.open(hostUrl+"/model/toImportModel?category="+category+"&navTabId="+op.navTabId, "importmodel", "流程模型导入", {width: 500, height: 400});
			}	
		}
	}
	function _baraddModelCategory() {
		var content = "<fieldset><legend>新增类型</legend><dl><dt>名称：</dt><dd><input  id='name' type='text' class='required' name ='name'/></dd></dl><dl><dt>代码：</dt><dd><input  id='category' type='text' class='required' remote='"
			+hostUrl
			+"/model/isExitsModelCategory' name ='category'/><span for='category' title='代码已存在' generated='false' class='error' style='display: none'></span></dd></dl><dl class='nowrap'><dt>描述：</dt><dd><textarea class='textInput' cols='30' rows='2' name='description'></textarea></dd></dl><dl><dd><div class='button'><div class='buttonContent'><button type='submit'>保存</button></div></div><div class='button'><div class='buttonContent'><button type='reset'>重置</button></div></div></dd></dl></fieldset>";
		$(op.formSelector).html(content);
		$("#modelform").attr("action", hostUrl + "/model/saveModelCategory");
	}
	
	function _baraddModel() {
		var tree = $("div#modeltree");
		var $a = tree.find("div.selected a");
		var category = $a.attr("category");
		if(category){
			var content = "<fieldset><input  id='category' type='hidden' value='{category}' name ='category'/><legend>新增流程模型</legend><dl><dt>名称：</dt><dd><input  id='name' type='text' class='required' name ='name'/></dd></dl>"
				+"<dl class='nowrap'><dt>描述：</dt><dd><textarea class='textInput' cols='30' rows='2' name='description'></textarea></dd></dl><dl><dd><div class='button'><div class='buttonContent'><button type='submit'>保存</button></div></div><div class='button'><div class='buttonContent'><button type='reset'>重置</button></div></div></dd></dl></fieldset>";
			$(op.formSelector).html(content.replaceAll("{category}", category));
			$("#modelform").attr("action", hostUrl + "/model/saveModel");
		}else{
			alertMsg.error("请选择一个类型节点进行操作!");
		}
	}
	
	function _resetModelDetail() {
		$(op.formSelector).html("");
	}
})(jQuery);